package com.rc.evcharger.task;

import com.rc.evcharger.common.base.BaseTask;
import com.rc.evcharger.common.mq.MqProducer;
import com.rc.evcharger.common.utils.DataUtil;
import com.rc.evcharger.common.utils.TimeoutUtil;
import io.netty.util.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

import static com.rc.evcharger.common.utils.DataUtil.SPLIT;

/**
 * 资源占用费短信通知
 * Create by Yang on 2018/11/22
 */
@Component
public class OccupySmsTask extends SupperTask implements BaseTask {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    final int TASK_SEND_MINUTES = 3;

    @Autowired
    private MqProducer mqCommandProducer;

    @Override
    public void runTask(Timeout timeout) throws Exception {
        try {
            TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
            String key = timeoutUtil.getRtua(timeout);
            if (key == null) {
                return;
            }
            String[] keys = key.split(SPLIT);

            /**发送资源占用费短信通知**/
            //mqCommandProducer.send(MqConfig.app_occupy_sms_listener_key, keys[keys.length - 1]);

//            logger.warn("资源占用费短信通知: 执行的key为：" + key);

        } catch (Exception e) {
            logger.error("资源占用费短信通知runTask方法出错: ", e);
        }

    }

    @Override
    public void removeTask(String rtua) {

    }

    @Override
    public void removeTask(String... keys) {
        StringBuffer key = new StringBuffer();
        for (String str : keys) {
            if (str == null)
                continue;
            key.append(str).append(SPLIT);
        }

        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
        Timeout timeout = timeoutUtil.getTimeout(this.getClass().getName() + key);
        if (timeout != null) {
            timeout.cancel();
            timeoutUtil.removeTimeout(this.getClass().getName() + key);
            logger.warn("资源占用费短信通知定时器被删除" + this.getClass().getName() + key);
        }
    }

    @Override
    public void addTask(String rtua, String gunNum, Integer sendTime, TimeUnit tu) {
        TimeUnit timeUnit = TimeUnit.MINUTES;
        if (tu != null) {
            timeUnit = tu;
        }
        if (sendTime == null) {
            sendTime = this.TASK_SEND_MINUTES;
        }
        //从redis里根据
        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
        Timeout timeout = timer.newTimeout(this, sendTime, timeUnit);
        String key = DataUtil.getKey(rtua, gunNum);
        logger.warn("添加资源占用费短信通知定时器key" + this.getClass().getName() + key);
        timeoutUtil.addTimeout(this.getClass().getName() + key, timeout);
    }

    public boolean hasTask(String rtua, String gunNum, String orderNum) {
        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
        String key = DataUtil.getKey(rtua, gunNum, orderNum);
        Timeout timeout = timeoutUtil.getTimeout(this.getClass().getName() + key);
        if (timeout != null) {
            return true;
        }
        return false;
    }
}
